package com.xzx.annotation.condition;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2018/7/14 20:35
 */
public class LinuxCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // 判断是否是linux环境
        //能获取到ioc使用的beanfactory
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        //获取类加载器
        ClassLoader classLoader = context.getClassLoader();
        //获取到bean定义的注册类
        BeanDefinitionRegistry registry = context.getRegistry();
        //3、获取当前环境信息
        Environment environment = context.getEnvironment();
        String os = environment.getProperty("os.name");
        if (os != null && os.toLowerCase().contains("linux")) {
            return true;
        }
        return false;
    }
}
